package com.chainyoung.component_tmperson.ui.activity.account;

import android.content.Intent;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chainyoung.common.appcommon.entity.RecieveAccount;
import com.chainyoung.common.appcommon.entity.UploadFile;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.imageloader.ImageLoader;
import com.chainyoung.common.imageloader.glide.GlideImageConfig;
import com.chainyoung.common.utils.AppUtils;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.utils.Utils;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.Bank;
import com.chainyoung.component_tmperson.ui.mvp.contract.BindRecieveAccountContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.BindRecieveAccountPresenter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

//收款二维码页面
public class BindAccountCodeActivity extends BaseActivity<BindRecieveAccountPresenter> implements BindRecieveAccountContract.View {


    @BindView(R2.id.iv_code)
    ImageView ivCode;
    @BindView(R2.id.tv_remind_info)
    TextView tvRemindInfo;
    @BindView(R2.id.tv_submit)
    TextView tvSubmit;

    private RecieveAccount account;
    private int type;
    private ImageLoader mImageLoader;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        account = (RecieveAccount) getIntent().getSerializableExtra("account");
        type = getIntent().getIntExtra("type", 2);

        mImageLoader = Utils.getAppComponent().imageLoader();

        setCurrentTitle(String.format(getString(R.string.tmperson_add_recieve_code), type == 2 ? "支付宝" : "微信"));
        tvRemindInfo.setText(type == 2 ? R.string.tmperson_add_ali_remind : R.string.tmperson_add_wx_remind);

        if (account != null) {
            mImageLoader.loadImage(mContext,
                    GlideImageConfig
                            .builder()
                            .url(account.getReceivables_code())
                            .placeholder(R.mipmap.ic_add)
                            .imagerView(ivCode)
                            .build());
        }


    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_bind_account_code;
    }


    @OnClick(R2.id.iv_code)
    void selectCodeImg() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .maxSelectNum(1)
                .enableCrop(true)
                .glideOverride(160, 160)
                .compress(true)
                .compressSavePath(AppUtils.getImagePath())
                .freeStyleCropEnabled(true)
                .minimumCompressSize(100)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    private List<String> selectImgs = new ArrayList<>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
                    selectImgs.clear();
                    for (LocalMedia media : selectList) {
                        selectImgs.add(media.getCompressPath());
                        Log.i("图片-----》", media.getCompressPath());
                    }
                    if (!selectImgs.isEmpty()) {
                        Glide.with(mContext).load(selectImgs.get(0)).apply(RequestOptions.placeholderOf(R.mipmap.ic_default).error(R.mipmap.ic_default)).into(ivCode);
                    }
                    break;
            }
        }
    }


    @OnClick(R2.id.tv_submit)
    void submit() {
        if (!selectImgs.isEmpty()) {
            mPresenter.upLoadCodeImg(selectImgs.get(0), 2);//上传收款二维码
        } else {
            finish();
        }
    }


    @Override
    public void onSetRecieveAccountSuccess(String data) {
        ToastUtil.showToast(data);
        finish();
    }

    @Override
    public void onUpLoadCodeSuccess(UploadFile uploadFile) {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("type", type);
        map.put("receivables_code", uploadFile.getUrl());
        mPresenter.setRecieveAccount(map);
    }


    @Override
    public void onGetBanks(List<Bank> data) {

    }


}
